//
//  EmptyViewController.m
//  Voxper
//
//  Created by Erphan on 2/24/11.
//  Copyright 2011 Smonte. All rights reserved.
//

#import "EmptyViewController.h"
#import "ConfigurationViewController.h"
#import "VoxperAppDelegate.h"
#import "UIManager.h"
#import "Settings.h"
#import "SipManager.h"
#import "RecentCallItem.h"
#import "SettingsMainViewController.h"

@interface EmptyViewController(Private)

- (void)showConfigurations:(id)sender;

@end

@implementation EmptyViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

- (void)showConfigurations:(id)sender {
    SettingsMainViewController *settingsMainViewController = [[SettingsMainViewController alloc] initWithNibName:@"SettingsMainViewController" bundle:[NSBundle mainBundle]];
    
    [self.navigationController pushViewController:settingsMainViewController animated:YES];
    [settingsMainViewController release];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	//[self.navigationController setNavigationBarHidden:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationReceived:) name:@"SipRegistration" object:nil];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered target:self action:@selector(showConfigurations:)] autorelease];
    
    app = [UIApplication sharedApplication].delegate;
    [[self navigationItem] setTitle:APP_NAME];
    
    [m_lblStatus setText:[NSString stringWithFormat:@"VN#: %@", [app.uiManager.settings getUserName]]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationReceivedForRecentCalls:) name:@"RecentCalls" object:nil];
}

- (void)notificationReceived:(NSNotification *)note {	
	if ([note object]) {
		NSString *strInfo = (NSString *)[note object];
        //m_lblStatus.text = strInfo;
		if ([strInfo isEqualToString:@"OK"]) {
			[m_txtStatus setImage:[UIImage imageNamed:@"sip_green.png"] forState:UIControlStateNormal];
            //[app.manager makeCall:@"66665555"];
		} else {
			[m_txtStatus setImage:[UIImage imageNamed:@"sip_yellow.png"] forState:UIControlStateNormal];
		}
		
	} else {
		[m_txtStatus setImage:[UIImage imageNamed:@"sip_grey.png"] forState:UIControlStateNormal];
	}
}

- (void)notificationReceivedForRecentCalls:(NSNotification *)note {
	if ([note object]) {
		NSString *strInfo = (NSString *)[note object];
		if ([strInfo isEqualToString:@"wait"]) {
			[m_tableView setUserInteractionEnabled:NO];
		} else {
			[m_tableView reloadData];
			[m_tableView setUserInteractionEnabled:YES];
		}
	}
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [m_lblStatus release];
    m_lblStatus = nil;
    [m_txtStatus release];
    m_txtStatus = nil;
    [m_tableView release];
    m_tableView = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [m_lblStatus release];
    [m_txtStatus release];
    [m_tableView release];
    [super dealloc];
}

#pragma mark UITableViewDelegates


- (NSInteger)numberOfSectionsInTableView:(UITableView *)_tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section {
	return [[app.uiManager getRecentCalls] count];
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"RecentCallCell";
	
	UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
	
	RecentCallItem *object;
	
	object = [[app.uiManager getRecentCalls] objectAtIndex:indexPath.row];
	
	
	if (object) {
		if ([object.Name length] > 0) {
			cell.textLabel.text = object.Name;
		} else {
			cell.textLabel.text = object.PhoneNumber;
		}
        
		NSString *strDetail = @"";
		
		if (object.CallType == 0) {
			strDetail = [NSString stringWithString:@"Outgoing"];
		} else if (object.CallType == 1) {
			strDetail = [NSString stringWithString:@"Missed"];
			cell.detailTextLabel.textColor = UIColorFromRGB(0xa80000);
		} else {
			strDetail = [NSString stringWithString:@"Answered"];
		}
		
//		if (object.Duration > 0 && object.CallType != 1) {
//			int forHours = object.Duration / 3600, 
//			remainder = object.Duration % 3600, 
//			forMinutes = remainder / 60, 
//			forSeconds = remainder % 60;
//			
//			strDetail = [NSString stringWithFormat:@"%@ %@", strDetail, [NSString stringWithFormat:@"Duration: %d:%d:%d", forHours, forMinutes, forSeconds]];
//		}
        
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:object.CallTime];
		NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
		[dateFormat setDateFormat:@"h:mm a MM/dd"];
		NSString *dateString = [dateFormat stringFromDate:date];
		//cell.TimeLabel.text = dateString;
        strDetail = [NSString stringWithFormat:@"%@ %@", strDetail, dateString];
		[dateFormat release];
		
		cell.detailTextLabel.text = strDetail;
	}
	
    return cell;
}
@end
